home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 002 / bsort.arc / SORT.M < prev   
Text File  |  1987-06-05  |  4KB  |  140 lines

  1. ;**  Sort
  2. ;**
  3. ;**      Brief macro by Keith Harp
  4. ;**
  5. ;**   Sort the marked lines, using the marked columns as keys.
  6. ;**   Works best if a COL_MARK is used instead of a normal mark.
  7. ;**   I.e.  You can see what's going on better.
  8.  
  9. ;**        Modified to account for a block specified by lower right-hand
  10. ;** corner to upper left-hand corner and visa versa and to allow for
  11. ;** tabs preceding the block to sort on.
  12. ;** Lew Paper
  13. ;** 6/5/87
  14.  
  15. (macro sort
  16.    (
  17.         (int            start_line
  18.                         start_col
  19.                         stop_line
  20.                         stop_col
  21.                   string_len
  22.         )
  23.          (if (! (inq_marked))
  24.             (message "No marked area.")
  25.         ;else
  26.             (
  27.             (save_position)
  28.  
  29.             ;** Figure out boundaries
  30.  
  31.             (inq_position start_line start_col)
  32.                 (swap_anchor)
  33.                 (inq_position stop_line stop_col)
  34.  
  35. ;    LP modification to correct error in next line and to allow for all 4
  36. ;    possible column block specifications
  37. ;            (= string_len (- stop_col start_col) 1)
  38.                 (if (> start_col stop_col)
  39. ;  End LP modification
  40.                 (= string_len (- start_col stop_col))
  41.                 ;else
  42.                 (= string_len (- stop_col start_col))
  43.                 )
  44.                 (++ string_len)                ; LP modification
  45.             (raise_anchor)
  46.             (message "Sorting...")
  47.  
  48.             (if (!= start_line stop_line)
  49.                    (if (|| 
  50.                       (< stop_line start_line)
  51.                       (&& (== stop_line start_line) (< stop_col start_col))
  52.                    )  
  53. ;    LP modification to allow for all 4 possible column block specifications
  54. ;                  (_dosort stop_line start_line stop_col (- 1 string_len))
  55.                   (_dosort stop_line start_line stop_col string_len)
  56. ;  End LP modification
  57.                ;else
  58. ;    LP modification to allow for all 4 possible column block specifications
  59. ;                  (_dosort start_line stop_line start_col (+ 1 string_len))
  60.                   (_dosort start_line stop_line start_col string_len)
  61. ;  End LP modification
  62.                    );endif
  63.             );endif
  64.  
  65.             (restore_position)
  66.             (message "Sort Done")
  67.          ) ;end else
  68.       ); end if
  69.    )
  70. )
  71.  
  72. ;** _dosort
  73. ;**
  74. ;**  Do insertion sort on the current buffer.
  75. #define FALSE 0
  76. #define TRUE 1
  77. (macro _dosort
  78.    (
  79.       (int        start_line
  80.                   stop_line
  81.                   start_col
  82.                   string_len
  83.  
  84.                   notfound
  85.                   total_len
  86.                   next_line
  87.                   next_linex100   
  88.                   stop_linex100
  89.                   cur_line
  90.       )
  91.       (string     first_key
  92.                   first_line
  93.       )
  94.       
  95.       (get_parm 0 start_line)
  96.       (get_parm 1 stop_line)
  97.       (get_parm 2 start_col)
  98.       (get_parm 3 string_len)
  99.  
  100.       (= next_line stop_line)
  101.       (= total_len (- start_line stop_line))
  102.       (= next_linex100  (* next_line 100))
  103.       (= stop_linex100 next_linex100)
  104.       (while (>= (= cur_line (-- next_line)) start_line)
  105.          (
  106.             (move_abs cur_line 1)
  107.             (= first_line (read))
  108. ; LP modification to correct for tabs
  109. ;            (= first_key (substr first_line start_col string_len))
  110.                 (move_abs cur_line start_col)
  111.                 (= first_key (read string_len))
  112. ; End LP modification
  113.             (move_abs (++ cur_line) start_col)
  114.             (if (> first_key (read string_len))
  115.                (
  116.                   (= notfound TRUE)
  117.                   (while (&& (<= (++ cur_line) stop_line) notfound)
  118.                      (
  119.                         (move_abs cur_line start_col)
  120.                         (= notfound (> first_key (read string_len)))
  121.                      )
  122.                   );end while
  123.                   (if (! notfound) (-- cur_line))
  124.                   (move_abs cur_line 1)
  125.                   (insert first_line)
  126.                   (move_abs next_line 1)
  127.                   (delete_line)
  128.                )
  129.             );end if
  130.             (message "Sorted %d%s" 
  131.                (/ (- (-= next_linex100 100) stop_linex100) total_len)
  132.                "%"
  133.             )
  134.          )
  135.       ); end while
  136.  
  137.    )
  138. ); macro _dosort
  139.  
  140.